gusucode.com > ACELP语音压缩的源代码 > ACELP语音压缩的源代码/Codec8000cpp SDK/Demo/Examples/Decoder8.cpp

    //  Example of VIMAS Speech codec DLL using 
//  Copyright VIMAS Technologies,  1998-2000
//  www.vimas.com
//  vimas@info.kiev.ua

#include <windows.h>
#include "codec.h"
#include <iostream.h>
#include <io.h>
#include <stdio.h>
#define SHORT short int
#define FORMAT 1  // 1 - WAV format, 0 - PCM
#define LH 23     // wav header size ( 16 bit words )
#define FILE_SINT0 "sint.pcm" // output file name, if PCM format
#define FILE_SINT1 "sint.wav" // output file name, if WAV format

#define FILE_PRM  "C:\\test_codec\\compres.prm"

typedef acelp (WINAPI *InitProc) ( SHORT mode );
typedef void (WINAPI *DeInitProc) ( acelp InterCurrent );
typedef acelp (WINAPI *DecoderProc) ( acelp, unsigned char *, SHORT * );


void main(void)
{
	HMODULE hLibrary = LoadLibrary( "VimasCodec.dll " );
	 if( hLibrary==NULL )
	 {
		cout<<"Can not load library\n";
		return;
	 }

	InitProc pInit = (InitProc)GetProcAddress( hLibrary, "Init" );
	 if( pInit==NULL )
	 {
		cout<<" Can not return the address of Init() function. \n";
		return;
	 }

	DeInitProc pDeInit = (DeInitProc)GetProcAddress( hLibrary, "DeInit" );
	 if( pDeInit==NULL )
	 {
		cout<<" Can not return the address of DeInit() function. \n";
		return;
	 }

	DecoderProc pDecoder = (DecoderProc)GetProcAddress( hLibrary, "Decoder" );
	 if( pDecoder==NULL )
	 {
		cout<<" Can not return the address of Decoder() function. \n";
		return;
	 }


	SHORT *sint;
	acelp InterCurrentD;
	acelp_frame CompressedFrame;
	long num_fr;
	long N;
	long NF; 

	FILE  *fsint;
	FILE  *fprm;

	if( (fprm=fopen(FILE_PRM,"rb"))==NULL)
	{
		cout<<"Can not open input file\n";
		return;
	}
	N=filelength(fileno(fprm));

	NF=N/COMPRESSED_FRAME_SIZE; 

	sint=new SHORT[FRAME_SIZE];

	if (FORMAT==1)  // if wav format  you want
	{
// Create wav header
	 SHORT hdr[ LH ];
     hdr[0] = 18770;
	 hdr[1] = 17990;
	 hdr[4] = 16727;
     hdr[5] = 17750;
  	 hdr[6] = 28006;
	 hdr[7] = 8308;
	 hdr[8] = 18;
	 hdr[9] = 0;
	 hdr[10]= 1;
	 hdr[11]= 1;
	 hdr[12]= 8000;
	 hdr[13]= 0;
	 hdr[14]= 16000;
	 hdr[15]= 0;
	 hdr[16]= 2;
	 hdr[17]= 16;
	 hdr[18]= 0;
	 hdr[19]= 24932;
	 hdr[20]= 24948;
   
	 N=2*(NF-1)*FRAME_SIZE;
	 hdr[21]= (SHORT)N;
	 hdr[22]= (SHORT)(N>>16);
	 N+=38;
     hdr[2]= (SHORT)N;
     hdr[3]= (SHORT)(N>>16); 

	 fsint = fopen( FILE_SINT1, "wb" );
// Write wav header
	 fwrite( hdr, sizeof(SHORT), LH, fsint );

	}
	else// if non-WAV format. We use  PCM format
	{
	  fsint = fopen( FILE_SINT0, "wb" );
	}

	InterCurrentD = (*pInit)(0);
    if(!InterCurrentD)
		return;

	for( num_fr=0; num_fr<NF-1; num_fr++ )
	{
     // read compressed frame	
	 fread( CompressedFrame, sizeof( acelp_frame ), 1, fprm );  
     // decode compressed frame
	 (*pDecoder)(InterCurrentD,  CompressedFrame, sint);
 	 // Write decompressed file
    fwrite( sint, sizeof(SHORT), FRAME_SIZE, fsint );
	}


	(*pDeInit)(InterCurrentD); 

	fclose(fprm);
    fclose( fsint );

	delete[] sint;

}